package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class CreateArticleStaticListener {

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @KafkaListener(topics = WmNewsMessageConstants.ARTICLE_STATIC_URL_CREATE_TOPIC)
    public void onMessage(String message){
        if(StringUtils.isNotBlank(message)){
            System.out.println(message);
            Map map = JSON.parseObject(message, Map.class);
            String articleId = (String) map.get("articleId");
            log.info("静态文件生成-接收的文章id:{}",articleId);
            articleFreemarkerService.buildArticleToMinIO(Long.valueOf(articleId));
        }
    }
}
